package cn.com.cfae.iras.doc.analyze.parse.extractor;

import cn.com.cfae.iras.doc.analyze.keys.ExtractMethod;
import cn.com.cfae.iras.doc.analyze.parse.GsonUtil;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.kv.TextLayout;
import cn.com.cfae.iras.doc.analyze.parse.model.*;
import cn.com.cfae.iras.doc.analyze.parse.tokenizer.FXJGTokenizer;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * M0/BM0抽取
 * 信用评级机构、信用评级在一块，需要处理
 */
public class M0Extractor extends MTableExtractor {

    private static final Logger logger = LoggerFactory.getLogger(M0Extractor.class);

    private FXJGTokenizer fxjgTokenizer = FXJGTokenizer.getInstance();

    @Override
    public void extract() {
        try {
            extractM0();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    public M0Extractor() {
        super();
    }

    private void extractM0() throws Exception {
        long t1 = System.currentTimeMillis();
        logger.info("开始抽取：{}章数据，表格类型：{}......", getChapterModel().getChapterId(), getDocumentModel().getTableType());
        logger.info("抽取章节：{}，Step1、抽取【发行人】基础数据......", getChapterModel().getChapterId());

        try {
            extractM0Fxr();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        logger.info("抽取章节：{}，Step1、抽取【发行人】基础数据完成。", getChapterModel().getChapterId());
        logger.info("抽取章节：{}，Step2、抽取【发行条款】信息......", getChapterModel().getChapterId());
        try {
            extractM0Fxtk();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        logger.info("抽取章节：{}，Step2、抽取【发行条款】基础数据完成。", getChapterModel().getChapterId());
        logger.info("抽取章节：{}，Step3、抽取【信用评级机构】基础数据......", getChapterModel().getChapterId());
        try {
            extractM0Xypjjg();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        logger.info("抽取章节：{}，Step3、抽取【信用评级机构】基础数据完成。", getChapterModel().getChapterId());
        logger.info("抽取章节：{}，Step4、抽取【信用增进机构】基础数据......", getChapterModel().getChapterId());
        try {
            extractM0Xyzjjg();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        logger.info("抽取章节：{}，Step4、抽取【信用增进机构】基础数据完成。", getChapterModel().getChapterId());

        logger.info("抽取章节：{}，Step5、抽取【主承销商】基础数据......", getChapterModel().getChapterId());
        try {
            extractM0Zcxs2();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        logger.info("抽取章节：{}，Step5、抽取【主承销商】基础数据完成。", getChapterModel().getChapterId());
        logger.info("抽取章节：{}，Step6、抽取【重要提示】基础数据......", getChapterModel().getChapterId());
        try {
            extractM0Zyts();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        logger.info("抽取章节：{}，Step6、抽取【重要提示】基础数据完成。", getChapterModel().getChapterId());
        logger.info("抽取章节：{}，Step7、抽取【目录结构】基础数据......", getChapterModel().getChapterId());
        logger.info("目录结构已在分析目录结构时构建，此处跳过。");
        logger.info("抽取章节：{}，Step7、抽取【目录结构】基础数据完成。", getChapterModel().getChapterId());
        long t2 = System.currentTimeMillis();
        logger.info("抽取：{}章数据完成，表格类型：{}，耗时：{}毫秒。", getChapterModel().getChapterId(), getDocumentModel().getTableType(), (t2 - t1));
    }


    private void extractM0Fxr() {
        PageModel pageModel = getChapterModel().getPageModelList().get(0);
        if (pageModel != null) {
            //如果为表格数据，构造过滤器，过滤表格数据，从剩余文本中提取业务指标
            ExtractBizModel extractBizModel = getChapterModel().getModel(FXRModel.MODEL_NAME);
            ExtractMethod[] extractMethods = extractBizModel.getExtractMethods();
            fxjgTokenizer.loadCoreSynonyms(extractBizModel);
            ExtractMethod extractMethod = extractMethods[0];
            List<String> extractTableDataList = null;
            switch (extractMethod) {
                case TEXT:
                    break;
                case KV:
                    try {
                        extractTableDataList = extractPdfByTable(new String[]{getDocumentModel().getPdfFilePath(), "-l", "-p", String.valueOf(pageModel.getPageNumber()), "-a", pageModel.getDefaultPageArea().getLayout(), "-f", "JSON"});
                    } catch (Exception e) {
                        logger.error(e.getMessage(), e);
                    } finally {
                    }
                    if (CollectionUtils.isEmpty(extractTableDataList)) {
                        logger.info("抽取{}章发行人，数据格式为文本形式，适用默认文本值进行分析，分析文本：{}。", getChapterModel().getChapterId(), pageModel.getJsonTextData());
                        JsonArray dataArray = getPageDataArray(pageModel);
                        for (int j = 0; j < dataArray.size(); j++) {
                            JsonArray lineArray = dataArray.get(j).getAsJsonArray();
                            String combineLine = combineTextJsonArray(lineArray);
                            if (isEmpty(combineLine)) {
                                continue;
                            }
                            if (j == dataArray.size() - 1) {
                                if (isBottomPageNumber(lineArray)) {
                                    break;
                                }
                            }
                            logger.info("抽取{}章发行人业务模型，分析文本：{}。", getChapterModel().getChapterId(), combineLine);
                            if (isCorpName(combineLine)) {
                                ExtractItem fxrExtractItem = extractBizModel.getExtractItem("FXRMC");
                                logger.info("抽取{}章发行人业务模型，分析到发行人名称：{}。", getChapterModel().getChapterId(), combineLine);
                                if (fxrExtractItem.getKey() != null) {
                                    continue;
                                }
                                KVText key = new KVText();
                                key.setText(fxrExtractItem.getSynonyms()[0]);
                                key.add(createKVTextLayout(pageModel, lineArray));
                                key.getTextLayout().setText(key.getText());
                                key.setPageNumber(pageModel.getPageNumber());
                                fxrExtractItem.setKey(key);
                                KVText value = new KVText();
                                value.setText(combineLine);
                                value.add(createKVTextLayout(pageModel, lineArray));
                                value.getTextLayout().setText(value.getText());
                                value.setPageNumber(pageModel.getPageNumber());
                                fxrExtractItem.setValue(value);
                                continue;
                            }
                            ExtractItem targetExtractItem = fxjgTokenizer.splitWords(extractBizModel, combineLine);
                            if (targetExtractItem != null) {
                                if (isDefaultKV(combineLine)) {
                                    String[] splits = combineLine.split("\\:");
                                    if (splits.length > 1) {
                                        setKV(lineArray, targetExtractItem);
                                        logger.info("抽取{}章发行人业务模型，分析到发行人相关指标信息：{}，分析文本。", getChapterModel().getChapterId(), targetExtractItem.getItemName(), combineLine);
                                        continue;
                                    }
                                }
                            }
                        }
                    } else {
                        logger.info("抽取{}章发行人业务模型，数据格式为表格形式，适用表格方式进行抽取，获取表格值：{}。", getChapterModel().getChapterId(), pageModel.getJsonTextData());
                        for (int i = 0; i < extractTableDataList.size(); i++) {
                            JsonElement pageTableData = str2Json(extractTableDataList.get(i));
                            JsonObject pageData = pageTableData.getAsJsonArray().get(0).getAsJsonObject();
                            JsonArray dataArray = pageData.get("data").getAsJsonArray();
                            for (int j = 0; j < dataArray.size(); j++) {
                                JsonArray itemArray = dataArray.get(j).getAsJsonArray();
                                if (itemArray.size() > 1) {
                                    JsonObject keyJsonObject = findValidTableCell(itemArray);
                                    if (keyJsonObject == null) {
                                        continue;
                                    }
                                    String key = keyJsonObject.get("text").getAsString();
//                                    String key = itemArray.get(0).getAsJsonObject().get("text").getAsString();
//                                    String value = itemArray.get(1).getAsJsonObject().get("text").getAsString();
//                                    String value = itemArray.get(1).getAsJsonObject().get("text").getAsString();
                                    ExtractItem targetExtractItem = fxjgTokenizer.splitWords(extractBizModel, key);
                                    if (targetExtractItem != null) {
                                        setTableKV(itemArray, targetExtractItem);
                                        setExtractItemPageNumber(pageModel, targetExtractItem);
                                        logger.info("抽取{}章信用评级机构业务模型，分析到信用评级机构相关信息，指标名称：{}，分析文本：{}。", getChapterModel().getChapterId(), targetExtractItem.getSynonyms()[0], combineTextJsonArray(itemArray));
                                        continue;
                                    }
                                }
                            }
                        }

                        JsonArray dataArray = getPageDataArray(pageModel);
                        for (int j = 0; j < dataArray.size(); j++) {
                            JsonArray lineArray = dataArray.get(j).getAsJsonArray();
                            String combineLine = combineTextJsonArray(lineArray);
                            if (isEmpty(combineLine)) {
                                continue;
                            }
                            if (j == dataArray.size() - 1) {
                                if (isBottomPageNumber(lineArray)) {
                                    break;
                                }
                            }
                            if (isInner3(lineArray, pageModel)) {
                                continue;
                            }
                            logger.info("抽取{}章发行人业务模型，原文本值：{}。", getChapterModel().getChapterId(), combineLine);
                            if (isCorpName(combineLine)) {
                                ExtractItem fxrExtractItem = extractBizModel.getExtractItem("FXRMC");
                                logger.info("抽取{}章发行人业务模型，分析到发行人名称：{}。", getChapterModel().getChapterId(), combineLine);
                                if (fxrExtractItem.getKey() != null) {
                                    logger.info("已分析到发行人业务模型，中断后续执行！！！");
                                    continue;
                                }
                                KVText key = new KVText();
                                key.setText(fxrExtractItem.getSynonyms()[0]);
                                key.add(createKVTextLayout(pageModel, lineArray));
                                key.getTextLayout().setText(key.getText());
                                fxrExtractItem.setKey(key);
                                key.setPageNumber(pageModel.getPageNumber());
                                KVText value = new KVText();
                                value.setText(combineLine);
                                value.add(createKVTextLayout(pageModel, lineArray));
                                value.getTextLayout().setText(value.getText());
                                value.setPageNumber(pageModel.getPageNumber());
                                fxrExtractItem.setValue(value);
                                continue;
                            }
                            try {
                                ExtractItem targetExtractItem = fxjgTokenizer.splitWords(extractBizModel, combineLine);
                                if (targetExtractItem != null) {
                                    if (isDefaultKV(combineLine)) {
                                        setKV(lineArray, targetExtractItem);
                                        setExtractItemPageNumber(pageModel, targetExtractItem);
                                        logger.info("抽取{}章发行人业务模型，分析发行人相关信息：{}，分析文本：{}。", getChapterModel().getChapterId(), targetExtractItem.getItemName(), combineLine);
                                        continue;
                                    }
                                }
                            } catch (Exception e) {
                                logger.error(e.getMessage(), e);
                            }
                        }
                    }
                    break;
                case TABLE:
                    break;
                case DEFAULT:
                    break;
            }
            fxjgTokenizer.unloadCoreSynonyms(extractBizModel);
            logger.info("抽取{}章发行人业务模型完成，输出发行人业务模型信息：{}。", getChapterModel().getChapterId(), obj2Json(extractBizModel));
        } else {
            logger.error("抽取{}章发行人，PDF文档：{}，扉页不存在，请检查！", getChapterModel().getChapterId(), getDocumentModel().getPdfFilePath());
        }
    }


    private void extractM0Fxtk() {
        PageModel pageModel = getChapterModel().getPageModelList().get(0);
        if (pageModel != null) {
            ExtractBizModel extractBizModel = getChapterModel().getModel(FXTKModel.MODEL_NAME);
            ExtractMethod[] extractMethods = extractBizModel.getExtractMethods();
            ExtractMethod extractMethod = extractMethods[0];
            List<String> extractTableDataList = null;
            fxjgTokenizer.loadCoreSynonyms(extractBizModel);
            switch (extractMethod) {
                case TEXT:
                    //TODO
                    break;
                case KV:
                    try {
                        extractTableDataList = extractPdfByTable(new String[]{getDocumentModel().getPdfFilePath(), "-l", "-p", String.valueOf(pageModel.getPageNumber()), "-a", pageModel.getDefaultPageArea().getLayout(), "-f", "JSON"});
                    } catch (Exception e) {
                        logger.error(e.getMessage(), e);
                    } finally {
                    }
                    if (CollectionUtils.isEmpty(extractTableDataList)) {
                        logger.info("抽取{}章发行条款业务模型，数据格式为文本形式，适用默认文本值进行分析，获取文本值：{}。", getChapterModel().getChapterId(), pageModel.getJsonTextData());
                        JsonArray dataArray = getPageDataArray(pageModel);
                        for (int j = 0; j < dataArray.size(); j++) {
                            JsonArray lineArray = dataArray.get(j).getAsJsonArray();
                            String combineLine = combineTextJsonArray(lineArray);
                            String combineLine2 = getNextLine(null, dataArray, j);
                            if (isEmpty(combineLine)) {
                                continue;
                            }
                            if (j == dataArray.size() - 1) {
                                if (isBottomPageNumber(lineArray)) {
                                    break;
                                }
                            }
                            logger.info("抽取{}章发行条款业务模型，原文本值：{}。", getChapterModel().getChapterId(), combineLine);
                            try {
                                ExtractItem zwrzgjmcExtractItem = extractBizModel.getExtractItem("ZWRZGJMC");
                                if (isProspectus(combineLine)) {
                                    logger.info("抽取{}章发行条款业务模型，分析到债务融资工具名称：{}。", getChapterModel().getChapterId(), combineLine);
                                    if (zwrzgjmcExtractItem.getKey() != null) {
                                        continue;
                                    }
                                    KVText key = new KVText();
                                    key.add(createKVTextLayout(pageModel, lineArray));
                                    key.setText(zwrzgjmcExtractItem.getSynonyms()[0]);
                                    key.getTextLayout().setText(key.getText());
                                    key.setPageNumber(pageModel.getPageNumber());
                                    zwrzgjmcExtractItem.setKey(key);

                                    KVText value = new KVText();
                                    value.add(createKVTextLayout(pageModel, lineArray));
                                    value.setText(combineLine);
                                    value.setPageNumber(pageModel.getPageNumber());
                                    value.getTextLayout().setText(value.getText());
                                    zwrzgjmcExtractItem.setValue(value);
                                    continue;
                                } else if (isProspectus(combineLine + combineLine2)) {
                                    logger.info("抽取{}章发行条款业务模型，分析到债务融资工具名称：{}。", getChapterModel().getChapterId(), combineLine);
                                    if (zwrzgjmcExtractItem.getKey() != null) {
                                        continue;
                                    }
                                    KVText key = new KVText();
                                    key.add(createKVTextLayout(pageModel, lineArray));
                                    key.setText(zwrzgjmcExtractItem.getSynonyms()[0]);
                                    key.getTextLayout().setText(key.getText());
                                    key.setPageNumber(pageModel.getPageNumber());
                                    zwrzgjmcExtractItem.setKey(key);
                                    KVText value = new KVText();
                                    value.add(createKVTextLayout(pageModel, lineArray));
                                    value.setText(combineLine + combineLine2);
                                    value.setPageNumber(pageModel.getPageNumber());
                                    value.getTextLayout().setText(value.getText());
                                    zwrzgjmcExtractItem.setValue(value);
                                    continue;
                                }
                                ExtractItem targetExtractItem = fxjgTokenizer.splitWords(extractBizModel, combineLine);
                                if (targetExtractItem != null) {
                                    if (isDefaultKV(combineLine)) {
                                        setKV(lineArray, targetExtractItem);
                                        setExtractItemPageNumber(pageModel, targetExtractItem);
                                        logger.info("抽取{}章发行条款，分析到发行条款相关信息：{}，分析文本：{}。", getChapterModel().getChapterId(), targetExtractItem.getItemName(), combineLine);
                                        continue;
                                    }
                                }
                            } catch (Exception e) {
                                logger.error(e.getMessage(), e);
                            }
                        }
                    } else {
                        logger.info("抽取{}章发行条款业务模型，数据格式为表格形式，适用表格方式进行抽取，表格数据：{}。", getChapterModel().getChapterId(), pageModel.getJsonTextData());
                        for (int i = 0; i < extractTableDataList.size(); i++) {
                            JsonElement pageTableData = str2Json(extractTableDataList.get(i));
                            JsonObject pageData = pageTableData.getAsJsonArray().get(0).getAsJsonObject();
                            JsonArray dataArray = pageData.get("data").getAsJsonArray();
                            for (int j = 0; j < dataArray.size(); j++) {
                                JsonArray itemArray = dataArray.get(j).getAsJsonArray();
                                if (itemArray.size() > 1) {
                                    try {
                                        JsonObject keyJsonObject = findValidTableCell(itemArray);
                                        if (keyJsonObject == null) {
                                            continue;
                                        }
                                        String key = keyJsonObject.get("text").getAsString();
//                                        String key = itemArray.get(0).getAsJsonObject().get("text").getAsString();
                                        ExtractItem targetExtractItem = fxjgTokenizer.splitWords(extractBizModel, key);
                                        if (targetExtractItem != null) {
                                            setTableKV(itemArray, targetExtractItem);
                                            setExtractItemPageNumber(pageModel, targetExtractItem);
                                            logger.info("抽取{}章发行条款业务模型，分析到信用评级机构相关信息，指标名称：{}，分析文本：{}。", getChapterModel().getChapterId(), targetExtractItem.getSynonyms()[0], combineTextJsonArray(itemArray));
                                            continue;
                                        }
                                    } catch (Exception e) {
                                        logger.error(e.getMessage(), e);
                                    }
                                } else if (itemArray.size() == 1) {
                                    try {
                                        JsonObject keyJsonObject = findValidTableCell(itemArray);
                                        if (keyJsonObject == null) {
                                            continue;
                                        }
                                        String key = keyJsonObject.get("text").getAsString();
                                        ExtractItem targetExtractItem = fxjgTokenizer.splitWords(extractBizModel, key);
                                        if (targetExtractItem != null) {
                                            if (isDefaultKV(key)) {
                                                setKV(itemArray, targetExtractItem);
                                                setExtractItemPageNumber(pageModel, targetExtractItem);
                                                logger.info("抽取{}章发行条款，分析到发行条款相关信息：{}，分析文本：{}。", getChapterModel().getChapterId(), targetExtractItem.getItemName(), key);
                                                continue;
                                            }
                                        }
                                    } catch (Exception e) {
                                        logger.error(e.getMessage(), e);
                                    }
                                }
                            }
                        }
                        logger.info("抽取{}章发行条款业务模型，表格数据分析完成，继续分析剩余文本。", getChapterModel().getChapterId());
                        {
                            JsonArray dataArray = getPageDataArray(pageModel);
                            for (int j = 0; j < dataArray.size(); j++) {
                                JsonArray lineArray = dataArray.get(j).getAsJsonArray();
                                String combineLine = combineTextJsonArray(lineArray);
                                String combineLine2 = getNextLine(null, dataArray, j);
                                if (isEmpty(combineLine)) {
                                    continue;
                                }
                                if (j == dataArray.size() - 1) {
                                    if (isBottomPageNumber(lineArray)) {
                                        break;
                                    }
                                }
                                try {
                                    if (isInner3(lineArray, pageModel)) {
                                        continue;
                                    }
                                    logger.info("抽取{}章发行条款业务模型，原文本值：{}。", getChapterModel().getChapterId(), combineLine);
                                    if (isProspectus(combineLine)) {
                                        ExtractItem zwrzgjmcExtractItem = extractBizModel.getExtractItem("ZWRZGJMC");
                                        logger.info("抽取{}章发行条款业务模型，解析到债务融资工具名称：{}。", getChapterModel().getChapterId(), combineLine);
                                        if (zwrzgjmcExtractItem.getKey() != null) {
                                            continue;
                                        }
                                        KVText key = new KVText();
                                        key.add(createKVTextLayout(pageModel, lineArray));
                                        key.setText(zwrzgjmcExtractItem.getSynonyms()[0]);
                                        key.getTextLayout().setText(key.getText());
                                        key.setPageNumber(pageModel.getPageNumber());
                                        zwrzgjmcExtractItem.setKey(key);
                                        KVText value = new KVText();
                                        value.add(createKVTextLayout(pageModel, lineArray));
                                        value.setText(combineLine);
                                        value.setPageNumber(pageModel.getPageNumber());
                                        value.getTextLayout().setText(value.getText());
                                        zwrzgjmcExtractItem.setValue(value);
                                        value.setPageNumber(pageModel.getPageNumber());
                                        continue;
                                    } else if (isProspectus(combineLine + combineLine2)) {
                                        ExtractItem zwrzgjmcExtractItem = extractBizModel.getExtractItem("ZWRZGJMC");
                                        logger.info("抽取{}章发行条款，解析到债务融资工具名称：{}。", getChapterModel().getChapterId(), combineLine);
                                        if (zwrzgjmcExtractItem.getKey() != null) {
                                            continue;
                                        }
                                        KVText key = new KVText();
                                        key.add(createKVTextLayout(pageModel, lineArray));
                                        key.setText(zwrzgjmcExtractItem.getSynonyms()[0]);
                                        key.getTextLayout().setText(key.getText());
                                        key.setPageNumber(pageModel.getPageNumber());
                                        zwrzgjmcExtractItem.setKey(key);
                                        KVText value = new KVText();
                                        value.add(createKVTextLayout(pageModel, lineArray));
                                        value.setText(combineLine);
                                        value.setPageNumber(pageModel.getPageNumber());
                                        value.getTextLayout().setText(value.getText());
                                        zwrzgjmcExtractItem.setValue(value);
                                        value.setPageNumber(pageModel.getPageNumber());
                                        continue;
                                    }
                                    ExtractItem targetExtractItem = fxjgTokenizer.splitWords(extractBizModel, combineLine);
                                    if (targetExtractItem != null) {
                                        if (isDefaultKV(combineLine)) {
                                            setKV(lineArray, targetExtractItem);
                                            setExtractItemPageNumber(pageModel, targetExtractItem);
                                            logger.info("抽取{}章发行条款业务模型，分析到发行条款相关信息：{}，分析文本：{}。", getChapterModel().getChapterId(), targetExtractItem.getItemName(), combineLine);
                                            continue;
                                        }
                                    }
                                } catch (Exception e) {
                                    logger.error(e.getMessage(), e);
                                }
                            }
                        }
                    }
                    break;
                case TABLE:
                    break;
                case DEFAULT:
                    break;
            }
            fxjgTokenizer.unloadCoreSynonyms(extractBizModel);
        } else {
            logger.error("抽取{}章发行条款业务模型，PDF文档：{}，扉页不存在，请检查！", getChapterModel().getChapterId(), getDocumentModel().getPdfFilePath());
        }
    }

    /**
     * 信用评级机构，每章节每个模型都是分开的，后续考虑合并
     *
     * @param
     */
    private void extractM0Xypjjg() {
        PageModel pageModel = getChapterModel().getPageModelList().get(0);
        if (pageModel != null) {
            ExtractBizModel extractBizModel = getChapterModel().getModel(XYPJJGModel.MODEL_NAME);
            fxjgTokenizer.loadCoreSynonyms(extractBizModel);
            ExtractMethod[] extractMethods = extractBizModel.getExtractMethods();
            ExtractMethod extractMethod = extractMethods[0];
            List<String> extractTableDataList = null;
            switch (extractMethod) {
                case TEXT:
                    //TODO
                    pageModel.getJsonTextData();
                    break;
                case KV:
                    try {
                        extractTableDataList = extractPdfByTable(new String[]{getDocumentModel().getPdfFilePath(), "-l", "-p", String.valueOf(pageModel.getPageNumber()), "-a", pageModel.getDefaultPageArea().getLayout(), "-f", "JSON"});
                    } catch (Exception e) {
                        logger.error(e.getMessage(), e);
                    } finally {
                    }
                    if (CollectionUtils.isEmpty(extractTableDataList)) {
                        logger.info("抽取{}章信用评级机构业务模型，数据格式为文本形式，适用默认文本值进行分析，获取文本值：{}。", getChapterModel().getChapterId(), pageModel.getJsonTextData());
                        JsonArray dataArray = getPageDataArray(pageModel);
                        for (int j = 0; j < dataArray.size(); j++) {
                            JsonArray lineArray = dataArray.get(j).getAsJsonArray();
                            String combineLine = combineTextJsonArray(lineArray);
                            if (isEmpty(combineLine)) {
                                continue;
                            }
                            if (j == dataArray.size() - 1) {
                                if (isBottomPageNumber(lineArray)) {
                                    break;
                                }
                            }
                            logger.info("抽取{}章信用评级机构业务模型，原文本值：{}。", getChapterModel().getChapterId(), combineLine);
                            try {
                                ExtractItem targetExtractItem = fxjgTokenizer.splitWords(extractBizModel, combineLine);
                                if (targetExtractItem != null) {
                                    if (isDefaultKV(combineLine)) {
                                        setKV(lineArray, targetExtractItem);
                                        setExtractItemPageNumber(pageModel, targetExtractItem);
                                        logger.info("抽取{}章信用评级机构业务模型，分析到信用评级机构相关信息：{}，分析文本：{}，。", getChapterModel().getChapterId(), targetExtractItem.getItemName(), combineLine);
                                        continue;
                                    }
                                }
                            } catch (Exception e) {
                                logger.error(e.getMessage(), e);
                            }
                        }
                    } else {
                        logger.info("抽取{}章信用评级机构业务模型，数据格式为表格形式，适用表格方式进行抽取，获取表格值：{}。", getChapterModel().getChapterId(), pageModel.getJsonTextData());
                        for (int i = 0; i < extractTableDataList.size(); i++) {
                            JsonElement pageTableData = str2Json(extractTableDataList.get(i));
                            JsonObject pageData = pageTableData.getAsJsonArray().get(0).getAsJsonObject();
                            JsonArray dataArray = pageData.get("data").getAsJsonArray();
                            for (int j = 0; j < dataArray.size(); j++) {
                                JsonArray itemArray = dataArray.get(j).getAsJsonArray();
                                if (itemArray.size() > 1) {
                                    JsonObject keyJsonObject = findValidTableCell(itemArray);
                                    if (keyJsonObject == null) {
                                        continue;
                                    }
                                    String key = keyJsonObject.get("text").getAsString();
//                                    String key = itemArray.get(0).getAsJsonObject().get("text").getAsString();
                                    ExtractItem targetExtractItem = fxjgTokenizer.splitWords(extractBizModel, key);
                                    if (targetExtractItem != null) {
                                        setTableKV(itemArray, targetExtractItem);
                                        setExtractItemPageNumber(pageModel, targetExtractItem);
                                        logger.info("抽取{}章信用评级机构业务模型，分析到信用评级机构相关信息，指标名称：{}，分析文本：{}。", getChapterModel().getChapterId(), targetExtractItem.getSynonyms()[0], combineTextJsonArray(itemArray));
                                        continue;
                                    }
                                }
                            }
                        }
                        if (extractBizModel.getExtractItem("MC").getKey() != null) {
                            break;
                        }
                        logger.info("抽取{}章信用评级机构业务模型，表格数据分析完成，未能分析到信用评级机构，开始以文本形式分析。", getChapterModel().getChapterId());
                        JsonArray dataArray = getPageDataArray(pageModel);
                        for (int j = 0; j < dataArray.size(); j++) {
                            JsonArray lineArray = dataArray.get(j).getAsJsonArray();
                            String combineLine = combineTextJsonArray(lineArray);
                            if (isEmpty(combineLine)) {
                                continue;
                            }
                            if (j == dataArray.size() - 1) {
                                if (isBottomPageNumber(lineArray)) {
                                    break;
                                }
                            }
                            if (isInner3(lineArray, pageModel)) {
                                continue;
                            }
                            logger.info("抽取{}章信用评级机构业务模型，原文本值：{}。", getChapterModel().getChapterId(), combineLine);
                            try {
                                ExtractItem targetExtractItem = fxjgTokenizer.splitWords(extractBizModel, combineLine);
                                if (targetExtractItem != null) {
                                    if (isDefaultKV(combineLine)) {
                                        setKV(lineArray, targetExtractItem);
                                        setExtractItemPageNumber(pageModel, targetExtractItem);
                                        logger.info("抽取{}章信用评级机构业务模型，分析到信用评级机构相关信息：{}，分析文本：{}。", getChapterModel().getChapterId(), targetExtractItem.getItemName(), combineLine);
                                        continue;
                                    }
                                }
                            } catch (Exception e) {
                                logger.error(e.getMessage(), e);
                            }
                        }
                    }
                    break;
                case TABLE:
                    break;
                case DEFAULT:
                    break;
            }
            fxjgTokenizer.unloadCoreSynonyms(extractBizModel);
        } else {
            logger.error("抽取{}章信用评级机构业务模型，PDF文档：{}，扉页不存在，请检查！", getChapterModel().getChapterId(), getDocumentModel().getPdfFilePath());
        }
    }


    /**
     * 信用评级机构，每章节每个模型都是分开的，后续考虑合并
     *
     * @param
     */
    private void extractM0Xyzjjg() {
        PageModel pageModel = getChapterModel().getPageModelList().get(0);
        if (pageModel != null) {
            ExtractBizModel extractBizModel = getChapterModel().getModel(XYZJJGModel.MODEL_NAME);
            fxjgTokenizer.loadCoreSynonyms(extractBizModel);
            ExtractMethod[] extractMethods = extractBizModel.getExtractMethods();
            ExtractMethod extractMethod = extractMethods[0];
            List<String> extractTableDataList = null;
            switch (extractMethod) {
                case TEXT:
                    //TODO
                    pageModel.getJsonTextData();
                    break;
                case KV:
                    try {
                        extractTableDataList = extractPdfByTable(new String[]{getDocumentModel().getPdfFilePath(), "-l", "-p", String.valueOf(pageModel.getPageNumber()), "-a", pageModel.getDefaultPageArea().getLayout(), "-f", "JSON"});
                    } catch (Exception e) {
                        logger.error(e.getMessage(), e);
                    } finally {
                    }
                    if (CollectionUtils.isEmpty(extractTableDataList)) {
                        logger.info("抽取{}章信用增进机构业务模型，数据格式为文本形式，适用默认文本值进行分析，获取文本值：{}。", getChapterModel().getChapterId(), pageModel.getJsonTextData());
                        JsonArray dataArray = getPageDataArray(pageModel);
                        for (int j = 0; j < dataArray.size(); j++) {
                            JsonArray lineArray = dataArray.get(j).getAsJsonArray();
                            String combineLine = combineTextJsonArray(lineArray);
                            if (isEmpty(combineLine)) {
                                continue;
                            }
                            if (j == dataArray.size() - 1) {
                                if (isBottomPageNumber(lineArray)) {
                                    break;
                                }
                            }
                            logger.info("抽取{}章信用增进机构业务模型，原文本值：{}。", getChapterModel().getChapterId(), combineLine);
                            try {
                                ExtractItem targetExtractItem = fxjgTokenizer.splitWords(extractBizModel, combineLine);
                                if (targetExtractItem != null) {
                                    if (isDefaultKV(combineLine)) {
                                        setKV(lineArray, targetExtractItem);
                                        setExtractItemPageNumber(pageModel, targetExtractItem);
                                        logger.info("抽取{}章信用增进机构业务模型，分析到信用增进机构相关信息：{}，分析文本：{}，。", getChapterModel().getChapterId(), targetExtractItem.getItemName(), combineLine);
                                        continue;
                                    }
                                }
                            } catch (Exception e) {
                                logger.error(e.getMessage(), e);
                            }
                        }
                    } else {
                        logger.info("抽取{}章信用增进机构业务模型，数据格式为表格形式，适用表格方式进行抽取，获取表格值：{}。", getChapterModel().getChapterId(), pageModel.getJsonTextData());
                        for (int i = 0; i < extractTableDataList.size(); i++) {
                            JsonElement pageTableData = str2Json(extractTableDataList.get(i));
                            JsonObject pageData = pageTableData.getAsJsonArray().get(0).getAsJsonObject();
                            JsonArray dataArray = pageData.get("data").getAsJsonArray();
                            for (int j = 0; j < dataArray.size(); j++) {
                                JsonArray itemArray = dataArray.get(j).getAsJsonArray();
                                if (itemArray.size() > 1) {
                                    JsonObject keyJsonObject = findValidTableCell(itemArray);
                                    if (keyJsonObject == null) {
                                        continue;
                                    }
                                    String key = keyJsonObject.get("text").getAsString();
                                    ExtractItem targetExtractItem = fxjgTokenizer.splitWords(extractBizModel, key);
                                    if (targetExtractItem != null) {
                                        setTableKV(itemArray, targetExtractItem);
                                        setExtractItemPageNumber(pageModel, targetExtractItem);
                                        logger.info("抽取{}章信用增进机构业务模型，分析到信用评级机构相关信息，指标名称：{}，分析文本：{}。", getChapterModel().getChapterId(), targetExtractItem.getSynonyms()[0], combineTextJsonArray(itemArray));
                                        continue;
                                    }
                                }
                            }
                        }
                        if (extractBizModel.getExtractItem("MC").getKey() != null) {
                            break;
                        }
                        logger.info("抽取{}章信用增进机构业务模型，表格数据分析完成，未能分析到信用增进机构，开始以文本形式分析。", getChapterModel().getChapterId());
                        JsonArray dataArray = getPageDataArray(pageModel);
                        for (int j = 0; j < dataArray.size(); j++) {
                            JsonArray lineArray = dataArray.get(j).getAsJsonArray();
                            String combineLine = combineTextJsonArray(lineArray);
                            if (isEmpty(combineLine)) {
                                continue;
                            }
                            if (j == dataArray.size() - 1) {
                                if (isBottomPageNumber(lineArray)) {
                                    break;
                                }
                            }
                            if (isInner3(lineArray, pageModel)) {
                                continue;
                            }
                            logger.info("抽取{}章信用增进机构业务模型，原文本值：{}。", getChapterModel().getChapterId(), combineLine);
                            try {
                                ExtractItem targetExtractItem = fxjgTokenizer.splitWords(extractBizModel, combineLine);
                                if (targetExtractItem != null) {
                                    if (isDefaultKV(combineLine)) {
                                        setKV(lineArray, targetExtractItem);
                                        setExtractItemPageNumber(pageModel, targetExtractItem);
                                        logger.info("抽取{}章信用增进机构业务模型，分析到信用增进机构相关信息：{}，分析文本：{}。", getChapterModel().getChapterId(), targetExtractItem.getItemName(), combineLine);
                                        continue;
                                    }
                                }
                            } catch (Exception e) {
                                logger.error(e.getMessage(), e);
                            }
                        }
                    }
                    break;
                case TABLE:
                    break;
                case DEFAULT:
                    break;
            }
            fxjgTokenizer.unloadCoreSynonyms(extractBizModel);
        } else {
            logger.error("抽取{}章信用增进机构业务模型，PDF文档：{}，扉页不存在，请检查！", getChapterModel().getChapterId(), getDocumentModel().getPdfFilePath());
        }
    }

    /**
     * 主承销商
     *
     * @param
     */
    private void extractM0Zcxs2() {
        PageModel pageModel = getChapterModel().getPageModelList().get(0);
        if (pageModel != null) {
            ExtractBizModel extractBizModel = getChapterModel().getModel(ZCXSModel.MODEL_NAME);
            ExtractMethod[] extractMethods = extractBizModel.getExtractMethods();
            fxjgTokenizer.loadCoreSynonyms(extractBizModel);
            ExtractMethod extractMethod = extractMethods[0];
            List<String> extractTableDataList = null;
            switch (extractMethod) {
                case TEXT:
                    //TODO
                    pageModel.getJsonTextData();
                    break;
                case KV:
                    try {
                        extractTableDataList = extractPdfByTable(new String[]{getDocumentModel().getPdfFilePath(), "-l", "-p", String.valueOf(pageModel.getPageNumber()), "-a", pageModel.getDefaultPageArea().getLayout(), "-f", "JSON"});
                    } catch (Exception e) {
                        logger.error(e.getMessage(), e);
                    } finally {
                    }
                    if (CollectionUtils.isEmpty(extractTableDataList)) {
                        logger.info("抽取{}章主承销商业务模型，数据格式为文本形式，适用默认文本值进行分析，分析文本：{}。", getChapterModel().getChapterId(), pageModel.getJsonTextData());
                        JsonArray dataArray = getPageDataArray(pageModel);
                        for (int j = 0; j < dataArray.size(); j++) {
                            JsonArray lineArray = dataArray.get(j).getAsJsonArray();
                            String combineLine = combineTextJsonArray(lineArray);
                            if (isEmpty(combineLine)) {
                                continue;
                            }
                            if (j == dataArray.size() - 1) {
                                if (isBottomPageNumber(lineArray)) {
                                    break;
                                }
                            }
                            logger.info("抽取{}章主承销商业务模型，分析文本：{}。", getChapterModel().getChapterId(), combineLine);
                            try {
                                ExtractItem targetExtractItem = fxjgTokenizer.splitWords(extractBizModel, combineLine);
                                if (targetExtractItem != null) {
                                    if (isDefaultKV(combineLine)) {
                                        setKV(lineArray, targetExtractItem);
                                        setExtractItemPageNumber(pageModel, targetExtractItem);
                                        logger.info("抽取{}章主承销商业务模型，分析到到主承销商相关信息：{}，分析文本{}。", getChapterModel().getChapterId(), targetExtractItem.getItemName(), combineLine);
                                        continue;
                                    }
                                }
                            } catch (Exception e) {
                                logger.error(e.getMessage(), e);
                            }
                        }
                    } else {
                        logger.info("抽取{}章主承销商业务模型，数据格式为表格形式，适用表格方式进行抽取，表格数据：{}。", getChapterModel().getChapterId(), pageModel.getJsonTextData());
                        for (int i = 0; i < extractTableDataList.size(); i++) {
                            JsonElement pageTableData = str2Json(extractTableDataList.get(i));
                            JsonObject pageData = pageTableData.getAsJsonArray().get(0).getAsJsonObject();
                            JsonArray dataArray = pageData.get("data").getAsJsonArray();
                            for (int j = 0; j < dataArray.size(); j++) {
                                JsonArray itemArray = dataArray.get(j).getAsJsonArray();
                                if (itemArray.size() > 1) {
                                    JsonObject keyJsonObject = findValidTableCell(itemArray);
                                    if (keyJsonObject == null) {
                                        continue;
                                    }
                                    String key = keyJsonObject.get("text").getAsString();
                                    ExtractItem targetExtractItem = fxjgTokenizer.splitWords(extractBizModel, key);
                                    if (targetExtractItem != null) {
                                        setTableKV(itemArray, targetExtractItem);
                                        setExtractItemPageNumber(pageModel, targetExtractItem);
                                        logger.info("抽取{}章主承销商业务模型，分析到主承销商相关信息，指标名称：{}，分析文本：{}。", getChapterModel().getChapterId(), targetExtractItem.getSynonyms()[0], combineTextJsonArray(itemArray));
                                        continue;
                                    }
                                }
                            }
                        }
                        if (extractBizModel.getExtractItem("MC").getKey() != null) {
                            break;
                        }
                        logger.info("抽取{}章主承销商业务模型，表格数据分析完成，未能分析到主承销商信息，开始以文本形式分析。", getChapterModel().getChapterId());
                        JsonArray dataArray = getPageDataArray(pageModel);
                        for (int j = 0; j < dataArray.size(); j++) {
                            JsonArray lineArray = dataArray.get(j).getAsJsonArray();
                            String combineLine = combineTextJsonArray(lineArray);
                            if (isEmpty(combineLine)) {
                                continue;
                            }
                            if (j == dataArray.size() - 1) {
                                if (isBottomPageNumber(lineArray)) {
                                    break;
                                }
                            }
                            if (isInner3(lineArray, pageModel)) {
                                continue;
                            }
                            String combineLine2 = getNextLine(null, dataArray, j);
                            logger.info("抽取{}章主承销商业务模型，分析文本：{}。", getChapterModel().getChapterId(), combineLine);
                            try {
                                ExtractItem targetExtractItem = fxjgTokenizer.splitWords(extractBizModel, combineLine + combineLine2);
                                if (targetExtractItem != null) {
                                    if (isDefaultKV(combineLine)) {
                                        setKV(lineArray, targetExtractItem);
                                        setExtractItemPageNumber(pageModel, targetExtractItem);
                                        logger.info("抽取{}章主承销商业务模型，分析到主承销商相关信息：{}，分析文本：{}。", getChapterModel().getChapterId(), targetExtractItem.getItemName(), combineLine);
                                        continue;
                                    }
                                }
                            } catch (Exception e) {
                                logger.error(e.getMessage(), e);
                            }
                        }
                    }
                    break;
                case TABLE:
                    break;
                case DEFAULT:
                    break;
            }
            fxjgTokenizer.loadCoreSynonyms(extractBizModel);
        } else {
            logger.error("抽取{}章PDF文档：{}，扉页不存在，请检查！！！", getChapterModel().getChapterId(), getDocumentModel().getPdfFilePath());
        }
    }

    private void extractM0Zyts() {
        //重要提示默认抽取第二页
        PageModel pageModel = getChapterModel().getPageModelList().get(1);
        if (pageModel != null) {
            ExtractBizModel extractBizModel = getChapterModel().getModel(ZYTSModel.MODEL_NAME);
            ExtractMethod[] extractMethods = extractBizModel.getExtractMethods();
            ExtractMethod extractMethod = extractMethods[0];
            switch (extractMethod) {
                case TEXT:
                    JsonArray dataArray = getPageDataArray(pageModel);
                    List<KVText> kvTexts = new ArrayList<>();
                    for (int i = 0; i < dataArray.size(); i++) {
                        JsonArray elements = dataArray.get(i).getAsJsonArray();
                        KVText value = new KVText();
                        TextLayout vTextLayout = createKVTextLayout(pageModel, elements);
                        if (vTextLayout == null) {
                            continue;
                        }
                        value.add(vTextLayout);
                        value.getTextLayout().setPageNumber(pageModel.getPageNumber());
                        value.setText(combineTextJsonArray(elements));
                        kvTexts.add(value);
                    }
                    extractBizModel.getExtractItem("ZYTS").setValues(kvTexts);
                    logger.info("抽取{}章重要提示，分析定位到重要提示，输出文本信息：{}。", getChapterModel().getChapterId(), dataArray.toString());
                    break;
                case KV:
                    break;
                case TABLE:
                    break;
                case DEFAULT:
                    break;
            }
        } else {
            logger.error("抽取{}章重要提示，PDF文档：{}，重要提示不存在，请检查！！！", getChapterModel().getChapterId(), getDocumentModel().getPdfFilePath());
        }
    }
}
